package com.example.demo.mapper;

import com.example.demo.entity.Goods;
import com.example.demo.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author chenhuiyu
 * @Date 2023/11/30
 */
@Mapper
@Repository
public interface GoodsMapper {
    @Select("SELECT * FROM goods WHERE user_id = #{userId}")
    @Results(id = "Goods", value = {
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "userId", column = "user_id"),
            @Result(property = "name", column = "name"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time"),
            @Result(property = "flag", column = "flag"),
            @Result(property = "groupId", column = "group_id"),
    })
    List<Goods> findGoodsByUserId(Integer userId);

    @Update("<script>" +
            "UPDATE goods SET 1=1 " +
            "<if test = 'goodsList.name != null and goodsList.name !=\"\"'>" +
            ",name = #{goodsList.name}</if>" +
            "<if test = 'goodsList.flag != null and goodsList.flag !=\"\"'>" +
            ",flag = #{goodsList.flag}</if>" +
            " WHERE id = #{goodsList.id} " +
            "</script>")
    Long updateGoodsById(List<Goods> goodsList);

    @Select("SELECT * FROM goods WHERE user_id = #{userId} AND name = #{name}")
    @ResultMap("Goods")
    Goods findGoodsByUserIdAndName(@Param("userId") Integer userId, @Param("name") String name);

    @Insert("INSERT INTO goods (user_id, name,create_time,update_time) " +
            "VALUES (#{goods.userId},#{goods.name},#{goods.createTime},#{goods.updateTime})")
    Long insertGoods(@Param("goods") Goods goods);

    @Select("SELECT * FROM goods WHERE user_id = #{userId}")
    @MapKey("id")
    @ResultMap(value = "Goods")
    Map<String, Goods> findGoods(Integer userId);

    @Select("SELECT * FROM goods WHERE group_id = #{groupId}")
    @ResultMap("Goods")
    List<Goods> findGoodsListByGroupId(Integer groupId);

    @Select("<script>" +
            "SELECT * FROM goods WHERE 1=1 AND id IN" +
            "<foreach collection='goodsIdList' item='item' index='index' open='(' separator=',' close=')'>" +
            " #{item} " +
            "</foreach>" +
            "</script>")
    @ResultMap("Goods")
    List<Goods> findGoodsByIdList(@Param("goodsIdList") List<Integer> goodsIdList);

    @Select("<script>" +
            "SELECT * FROM goods WHERE 1=1" +
            "<if test='id != null'>" +
            " AND id = #{id}" +
            "</if>" +
            "<if test='userId != null'>" +
            " AND user_id = #{userId}" +
            "</if>" +
            "<if test='name != null'>" +
            " AND name = #{name}" +
            "</if>" +
            "<if test='groupId != null'>" +
            " AND group_id = #{groupId}" +
            "</if>" +
            "<if test='queryTimeType = 1'>" +
            "   <if test='startTime != null'>" +
            "    AND create_time >= #{startTime}" +
            "   </if>" +
            "   <if test='endTime != null'>" +
            "    AND #{endTime} >= create_time" +
            "   </if>" +
            "</if>" +
            "<if test='queryTimeType = 2'>" +
            "   <if test='startTime != null'>" +
            "    AND update_time >= #{startTime}" +
            "   </if>" +
            "   <if test='endTime != null'>" +
            "    AND #{endTime} >= update_time" +
            "   </if>" +
            "</if>" +
            "</script>")
    @ResultMap("Goods")
    List<Goods> findGoodsList(Goods goods);
}
